﻿/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2012 Junhua
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in the 
 * Software without restriction, including without limitation the rights to use, copy, 
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, subject to the 
 * following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
namespace Reading.Controls
{
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Controls.Primitives;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    /// <summary>
    /// The lockable Pivot extends the base Pivot control with a property that
    /// disables navigation between Pivot items.
    /// </summary>
    /// <QualityBand>Experimental</QualityBand>
    [TemplatePart(Name = HeadersListElement, Type = typeof(PivotHeadersControl))]
    [TemplatePart(Name = PivotItemPresenterElement, Type = typeof(ItemsPresenter))]
    [StyleTypedProperty(Property = ItemContainerStyleName, StyleTargetType = typeof(PivotItem))]
    public class ListPivot : Pivot
    {
        /// <summary>
        /// Headers list element template part.
        /// </summary>
        const string HeadersListElement = "HeadersListElement";

        /// <summary>
        /// Content element template part.
        /// </summary>
        const string PivotItemPresenterElement = "PivotItemPresenter";

        /// <summary>
        /// The item container style property name.
        /// </summary>
        const string ItemContainerStyleName = "ItemContainerStyle";

        public ListPivot()
            : base()
        {
            Touch.FrameReported += OnTouchFrameReported;
        }

        double s, x;

        void OnTouchFrameReported(object sender, TouchFrameEventArgs e)
        {
            var ps = e.GetTouchPoints(this);
            if (ps.Count > 0)
            {
                var p = ps[0];

                if (p.Action == TouchAction.Down)
                {
                    s = p.Position.X;
                    x = p.Position.X;
                    System.Diagnostics.Debug.WriteLine(string.Format("Down:{0}", x));
                }

                if (p.Action == TouchAction.Move)
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("IsHead : {0}, IsTail : {1}", IsHead, IsTail));

                    if (IsHead)
                    {
                        if (p.Position.X - x >= 0)
                        {
                            this.IsHitTestVisible = false;
                        }
                        else
                        {
                            this.IsHitTestVisible = true;
                            x = p.Position.X;
                        }

                        System.Diagnostics.Debug.WriteLine(string.Format("IsHead : {0}, IsHitTestVisible : {1}", IsHead, IsHitTestVisible));
                    }

                    if (IsTail)
                    {
                        if (p.Position.X - x <= 0)
                        {
                            this.IsHitTestVisible = false;
                            System.Diagnostics.Debug.WriteLine(string.Format("IsTail : {0}, IsHitTestVisible : {1}",IsTail, IsHitTestVisible));
                        }
                        else
                        {
                            this.IsHitTestVisible = true;
                            x = p.Position.X;
                            System.Diagnostics.Debug.WriteLine(string.Format("IsHead : {0}, IsTail : {1}", IsHead, IsTail));
                        }

                        System.Diagnostics.Debug.WriteLine(string.Format("IsTail : {0}, IsHitTestVisible : {1}", IsTail, IsHitTestVisible));
                    }


                }

                if (p.Action == TouchAction.Up)
                {
                    if (s - x > 50 && !this.IsHitTestVisible)
                    {
                        SelectedIndex += 1;
                    }
                    else if (s - x < -50 && !this.IsHitTestVisible)
                    {
                        SelectedIndex -= 1;
                    }

                    this.IsHitTestVisible = true;
                    System.Diagnostics.Debug.WriteLine(string.Format("Up:{0}, {1}", x, x - s));

                }
            }
        }

        #region IsHead DependencyProperty

        /// <summary>
        /// Gets or sets the data template that defines
        /// the content information.
        /// </summary>
        public bool IsHead { get; set; }
        /*{
            get { return (bool)GetValue(IsHeadProperty); }
            set { SetValue(IsHeadProperty, value); }
        }*/

        /// <summary>
        /// Identifies the IsHeadProperty dependency property.
        /// </summary>
        public static readonly DependencyProperty IsHeadProperty =
            DependencyProperty.Register("IsHead", typeof(bool), typeof(ListPivot), new PropertyMetadata(true, OnIsHeadPropertyChanged));

        /// <summary>
        /// PullPanelTemplate changed handler.
        /// </summary>
        /// <param name="obj">The dependency object.</param>
        /// <param name="e">The event information.</param>
        private static void OnIsHeadPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            ListPivot pivot = obj as ListPivot;
            if (pivot != null)
            {
                pivot.OnIsHeadPropertyChanged(e);
            }
        }

        private void OnIsHeadPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
        }

        #endregion

        #region IsTail DependencyProperty

        /// <summary>
        /// Gets or sets the IsTail that defines
        /// the content information.
        /// </summary>
        public bool IsTail { get; set; }
        /*{
             get { return (bool)GetValue(IsTailProperty); }
             set { SetValue(IsTailProperty, value); }
         }*/

        /// <summary>
        /// Identifies the IsTailProperty dependency property.
        /// </summary>
        public static readonly DependencyProperty IsTailProperty =
            DependencyProperty.Register("IsTail", typeof(bool), typeof(ListPivot), new PropertyMetadata(true, OnIsTailPropertyChanged));

        /// <summary>
        /// IsTail changed handler.
        /// </summary>
        /// <param name="obj">The dependency object.</param>
        /// <param name="e">The event information.</param>
        private static void OnIsTailPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            ListPivot pivot = obj as ListPivot;
            if (pivot != null)
            {
                pivot.OnIsTailPropertyChanged(e);
            }
        }

        private void OnIsTailPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
        }

        #endregion
    }
}
